/* Name: Nguyen Le Niem
nickname:Mark Integer
sdt:01646828033
*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
class phonghoc{
      private:

              char maphong[50];
              char* tenphong;
              int sochongoi;
      public:
             friend class Truong;
             friend ostream& operator <<(ostream& niemdeptrai, phonghoc a);
             friend istream& operator >>(istream& niem, phonghoc& a);
             phonghoc(){
                           strcpy(maphong,"");
                          tenphong=new char[50];
                          strcpy(tenphong,"");
                           sochongoi=0;
                          }
             void nhap();
             void in();
             };
            void phonghoc::nhap(){
                         cout<<"nhap ma phong:";cin>>maphong;
                         cout<<"\nnhap ten phong:";cin>>tenphong;
                         cout<<"\nso cho ngoi:";cin>>sochongoi;}
            void phonghoc::in(){
                       cout<<"("<<maphong<<","<<tenphong<<","<<sochongoi<<")";
                       cout<<endl;
                       cout<<"------------------------\n";
                       }
      ostream& operator <<(ostream& niemdeptrai, phonghoc a){
              niemdeptrai<<a.maphong<<" "<<a.tenphong<<" "<<a.sochongoi;
                       return niemdeptrai;


              }
      istream& operator >>(istream& niem, phonghoc& a){
             // cout<<"nhap ma phong:";
              niem>>a.maphong;
             // cout<<"nhap ten phong:";
              niem>>a.tenphong;
            //  cout<<"nhap so cho ngoi :";
              niem>>a.sochongoi;
              return niem;
              }


class Truong{
      private:
              char matruong[20];
              char* tentruong;
              char diachi[50];
              int slp;
              phonghoc* dsph[20];
      public:
              friend ostream& operator <<(ostream& niemdeptrai, Truong& a);
              friend istream& operator >>(istream& niem, Truong& a);
             Truong();
             void nhap();
             void in();
             void timphong(char&);
             };
      Truong::Truong(){
                       strcpy(matruong,"");
                       tentruong=new char[50];
                       strcpy(tentruong,"");
                       strcpy(diachi,"");
                       slp=0;}
      void Truong::nhap(){
           fflush(stdin);
           cout<<"nhap ma truong:";gets(matruong);
           cout<<"nhap ten truong:";gets(tentruong);
           cout<<"nhap dia chi:";gets(diachi);
           cout<<"nhap so luong phong:";cin>>slp;
           for(int i=0;i<slp;i++)
           {
                   cout<<"phong thu "<<i+1;cout<<endl;
                   dsph[i]=new phonghoc;
                   dsph[i]->nhap();
                   }
                   }
      void Truong::in(){
           cout<<"ma truong:"<<matruong;
           cout<<"\nten truong:"<<tentruong;
           cout<<"\ndia chi:"<<diachi;
           cout<<"\nso luong phong "<<slp;
           cout<<endl;
           for(int i=0;i<slp;i++)
                   dsph[i]->in();
                   }
     ostream& operator <<(ostream& niemdeptrai, Truong& a){
           niemdeptrai<<a.matruong<<endl;
           niemdeptrai<<a.tentruong<<endl;
           niemdeptrai<<a.diachi<<endl;
           niemdeptrai<<a.slp<<endl;
           for(int i=0;i<a.slp;i++){
                   niemdeptrai<<"  ";
                   niemdeptrai<<(*a.dsph[i]);}

            return niemdeptrai;
              }
    istream& operator >>(istream& niem, Truong& a){
             niem.ignore();
           niem.getline(a.matruong,20);
           niem.getline(a.tentruong,50);
           niem.getline(a.diachi,50);
           niem>>a.slp;
           for(int i=0;i<a.slp;i++)
           {
                   a.dsph[i]=new phonghoc();
                   niem>>(*a.dsph[i]);
                   }

              return niem;
                     }
      int main(){
          Truong a;
          Truong b;
          a.nhap();
          cout<<"luu file:\n";
          ofstream f1("niemfile1.txt");
          f1<<a;
          f1.close();
          cout<<"lay tu file:\n";
          ifstream f2("niemfile1.txt");
          f2>>b;
          b.in();
          f2.close();
          getch();
          }



